package cn.chenyumeng.blog.controller.admin;

import cn.chenyumeng.blog.dto.CommonResponse;
import cn.chenyumeng.blog.dto.ResultCode;
import cn.chenyumeng.blog.dto.UserDTO;
import cn.chenyumeng.blog.entity.User;
import cn.chenyumeng.blog.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * 博客管理登录控制器
 *
 * @program: blog->LoginController
 * @description: 博客管理登录控制器
 * @author: cg
 * @create: 2020-07-27 09:45
 **/
@RestController
@ResponseBody
@RequestMapping("admin/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public CommonResponse login(@NotBlank(message = "用户名不能为空") String username, @NotBlank(message = "密码不能为空") String password) {
        UserDTO user = userService.checkUser(username, password);
        if (user != null) {
            user.setPassword(null);
            return CommonResponse.ok(user);
        } else {
            return CommonResponse.build(ResultCode.USER_LOGIN_ERROR);
        }
    }

    @PostMapping("/logout")
    public CommonResponse logout() {
        return CommonResponse.ok();
    }

    @GetMapping("/getUserInfoById")
    public CommonResponse getUserInfoById(@RequestParam(name = "id") Long id) {
        User user = userService.queryById(id);
        if (user == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(user);
    }
}
